﻿using System;
using System.Drawing;

namespace ConvexPolygon
{
    class LineEquation
    {
        private double x1, x2, y1, y2;

        public LineEquation(PointF p1, PointF p2)
        {
            /*
             * line through points l10 and l11 is:
             * F(x y) = (y2-y1)x + (x1-x2)y + (x2*y1-x1*y2)
             *
             */

            x1 = (double)p1.X;
            x2 = (double)p2.X;
            y1 = (double)p1.Y;
            y2 = (double)p2.Y;
        }

        public double F(PointF p)
        {
            return (y2 - y1) * (double)p.X + (x1 - x2) * (double)p.Y + (x2 * y1 - x1 - y2);
        }

        public double SolveY(double x)
        {
            return (-x * y2 + x * y1 - x2 * y1 + x1 * y2) / (x1 - x2);
        }

        public double solveX(double y)
        {
            return (y * x1 - y * x2 + x2 * y1 - x1 * y2) / (-y2 + y1);
        }

        public double Slope()
        {
            if (x2 - x1 == 0)
                throw new Exception("Vertical line, slope is infinity.");
            return (y2-y1)/(x2-x1);
        }

        public override string ToString()
        {

            return "F(x,y)=" + (y2 - y1).ToString() + "*x+" + (x1 - x2).ToString() + "*y+" + (x2 * y1 - x1 * y2).ToString() + "=0";
        }

    }
}
